function Tt = t_predic_OMA( t, interp_fn, ts, varargin )
% T_PREDIC_OMA   t_predic tidal predictions that generates a TUV
% structure from tidal analysis of OMA_alpha (coefficients of OMA modes).
%
% This functions uses the T_tide toolbox of R. Pawlowicz et al.
% (http://www2.ocgy.ubc.ca/~rich/#T_Tide) to predict tides.
% 
% NOTE: t_predic must be on the path for this function to work.
%
% Usage: Ttide = t_predic_OMA(TIM,interp_fn,TIDESTRUC,prop,val...)
%
% Inputs
% ------
% TIM = a vector of times in datenum format for making predictions
% interp_fn = filename with modes interpolated on a grid.  See
%             interp_OMA_modes_to_grid for more details.
% TIDESTRUC = an array of tide structures for making predictions.  Generally
%             generated by t_tide_matrix applied to OMA_alpha.  Should have
%             the same number of elements as modes in OMA analysis.
% property,value,... = parameter name,value pairs for t_predic_matrix.  See
%                      that function for details. These may override
%                      prop,val pairs generated by this function (e.g.,
%                      'latitude' if nodal is true).
%
% Outputs
% -------
% Ttide = TUV structure with predicted tides. NOTE: This function
%         generates a rather basic TUV structure.  Extra metadata must be
%         filled in by hand later.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 	$Id: t_predic_OMA.m 519 2007-12-11 10:18:36Z dmk $	
%
% Copyright (C) 2007 David M. Kaplan
% Licence: GPL
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if exist( 't_tide' ) < 2
  error( [ mfilename ' - Generic t_tide function must be on path.' ] );
end

t = t(:)';

% Load modes data.
g = load( interp_fn, 'ux_interp_grid', 'uy_interp_grid', 'gridLonLat' );

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tide prediction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha = t_predic_matrix( t, ts, varargin{:} )';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generate TUV structure and fill
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Tt = TUVstruct( [size(g.gridLonLat,1),size(xx,2)], 0 );

Tt.ProcessingSteps{end+1} = mfilename;
Tt.OtherMetadata.(mfilename).varargin = varargin;
Tt.OtherMetadata.(mfilename).Type = 'tide, OMA';
  
Tt.OtherTemporalVars.OMA_alpha = alpha;

Tt.U = g.ux_interp_grid * alpha;
Tt.V = g.uy_interp_grid * alpha;
  
Tt.LonLat = g.gridLonLat;
Tt.TimeStamp = t;

